import APIConfig from '../config/api'
import {wxToPromise} from './wx'
import exceptionMessage from '../config/exception-message'
class Http {
  async request({url,data={},method="GET"}){
    const res = await wxToPromise("request",{
      url: APIConfig.baseUrl + url,
      method,
      data,
    })
    // 请求成功
    if(res.statusCode < 400) {
      return res.data.data
    }
    // 令牌过期
    if(res.statusCode === 401) {
      // 
      return
    }
    // 请求失败
    this._showError(res.data.error_code, res.data.message)
  }
  _showError(errorCode,message) {
    let title = exceptionMessage[errorCode] || message || "未知异常"
    title = typeof title === 'object'?Object.values(title).join(""):title
    wx.showToast({
      title,
      icon:'none',
      duration:3000
    })
  }
}
export default Http